import { HiddenRowColPlugin } from '../../../pugins/hidden-row-col.plugin';
import { OperateBase } from '../../common/operate-base';

export class HiddenRow extends OperateBase {
    protected container: HTMLElement;
    protected plugin: HiddenRowColPlugin
    constructor() {
        super();
    }
    public getDom() {
        this.container = document.createElement('div');
        this.container.setAttribute('class', this.containerClassName);
        this.container.innerHTML=`<span class="z-tools-icon icon iconfont icon-hiddenrow"></span>`;
        return this.container;
    }
    public selectChange(select: any, cells: any[]): void {
        
    }
    public afterDomInit(): void {
        this.plugin = this.zTableInstance.pluginManager.getPlugin(HiddenRowColPlugin.pluginName);
        this.zTableInstance.getHook().afterSelect.add(() => {
            this.toggleDisabled();
        });
        this.toggleDisabled();
        let iconEle = this.container.querySelector('.icon-hiddenrow') as HTMLElement;
        iconEle.addEventListener('click', () => {
            if(iconEle.classList.contains('z-table-icon-disabled')) {
                return;
            }
            let hiddenRows = this.plugin.getHiddenRows();
            let select = this.plugin.getSelectInfo();
            let hasVisualRow = false;
            for(let row = select.row; row < select.row + select.rowspan; row++) {
                if(hiddenRows.indexOf(row) === -1) {
                    hasVisualRow = true;
                    break;
                }
            }
            if(hasVisualRow) {
                this.plugin.hiddenRows();
            } else {
                this.plugin.cancelHiddenRows();
            }
            // if(this.plugin.getFreezeInfo().col !== -1) {
            //     this.plugin.cancelFreezeCol();
            //     this.toggleDisabled();
            // } else {
            //     let select = this.plugin.getSelectInfo();
            //     if(select !== null && select.col !== -1) {
            //         this.plugin.freezeCol(select.col);
            //         this.toggleDisabled();
            //     }
            // }
        });
    }
    protected toggleDisabled() {
        let select = this.plugin.getSelectInfo();
        let iconEle = this.container.querySelector('.icon-hiddenrow') as HTMLElement;
        if(select) {
            iconEle.classList.remove('z-table-icon-disabled');
        } else {
            iconEle.classList.add('z-table-icon-disabled');
        }
    }
}